package com.bakabase.animation.service.impl;

import com.bakabase.animation.domain.Animation;
import com.bakabase.animation.domain.dto.AnimationDto;
import com.bakabase.animation.domain.dto.AnimationHomeDto;
import com.bakabase.animation.domain.dto.FileDto;
import com.bakabase.animation.domain.dto.HomeDataDto;
import com.bakabase.animation.domain.vo.AnimationVo;
import com.bakabase.animation.mapper.AnimationMapper;
import com.bakabase.animation.service.AnimationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 22384
 * @description 针对表【a_animation】的数据库操作Service实现
 * @createDate 2024-10-20 20:51:10
 */
@Service
@Transactional(readOnly = true)
public class AnimationServiceImpl extends ServiceImpl<AnimationMapper, Animation>
        implements AnimationService {

    @Resource
    private AnimationMapper animationMapper;

    /**
     * 查询所有动画
     *
     * @param
     * @return
     */
    @Override
    public IPage<AnimationDto> selectList(Page<AnimationDto> page, AnimationVo vo) {
        return animationMapper.listAll(page, vo);
    }

    /**
     * 新增动画
     *
     * @param animation
     * @return
     */
    @Transactional
    public int insertNewAnimation(Animation animation) {
        int i = animationMapper.insertNewAnimation(animation);
        int j = 0;
        if (StringUtils.isNotBlank(animation.getCover())) {
            j = animationMapper.insertCover(animation.getId().toString(), animation.getCover());
        }
        return i + j;
    }

    /**
     * 修改动画
     *
     * @param animation
     * @return
     */
    @Transactional
    public int updateAnimation(Animation animation) {
        if (StringUtils.isNotBlank(animation.getCover()) && animation.getFileId() == null) {
            // 此时为之前没有封面，现在添加新的封面
            // 再插入中间表信息
            animationMapper.insertCover(animation.getId().toString(), animation.getCover());
        } else {
            // 正常修改封面
            // 先删除中间表信息
            animationMapper.deleteCover(animation.getId());
            // 再插入中间表信息
            animationMapper.insertCover(animation.getId().toString(), animation.getFileId());
        }


        return animationMapper.updateAnimation(animation);
    }

    /**
     * 获取单个动画详情
     *
     * @param id
     * @return
     */
    public AnimationDto selectOne(Long id) {
        AnimationDto animationDto = animationMapper.selectOneAnimation(id);
        // 单独查询封面
        FileDto dto = animationMapper.selectCover(id);
        if (dto!= null) {
            animationDto.setFileId(dto.getFileId());
            animationDto.setCover(dto.getFilePath());
        }
        return animationDto;
    }

    /**
     * 删除动画
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int deleteAnimation(Long id) {
        int i = animationMapper.deleteAnimation(id);
        // 删除中间表信息
        int j = animationMapper.deleteCover(id);
        return i + j;
    }

    /**
     * 获取主页动画数据展示对象
     * @return
     */
    @Override
    public HomeDataDto selectHomeDataList() {
        HomeDataDto homeDataDto = new HomeDataDto();
        // 查询TV动画(12)
        List<AnimationHomeDto> tvList = animationMapper.selectTVList();
        // 查询剧场版动画(6)
        List<AnimationHomeDto> movieList = animationMapper.selectMovieList();
        homeDataDto.setTvAnimations(tvList);
        homeDataDto.setMovieAnimations(movieList);
        return homeDataDto;
    }

}




